<script>
export default {
    data() {
        return {
            msgStr:"",
            conn:undefined,
            msgList:[]
        }
    },
    methods: {
        send(event) {
            if(event.keyCode){
                if(event.keyCode!=13){
                    return;
                }
            }
            this.conn.invoke("SendMessage", { messageStr: this.msgStr }).catch(function (err) {
                this.addToList({
                    user: "前端",
                    str: "err:" + err
                });
            });
            this.msgStr = "";
            event.preventDefault();
        },
        addToList(msg) {
            this.msgList.splice(0,0,msg)
        },
        init(conn){
            this.conn = conn;
            const page = this;
            this.conn.on("ReceiveMessage", function (msg) {
                page.addToList(msg);
            });
            this.conn.onclose(() => {
                page.addToList({
                    user: "前端",
                    str: "已被后端断开连接，可能是由于没登录"
                });
            });
            this.conn.start().then(function () {
                page.addToList({
                    user: "前端",
                    str: "成功与后端连接"
                });
            }).catch(function (err) {
                page.addToList({
                    user: "前端",
                    str: "err:" + err
                });
            });
        }
    },
    mounted() {
        //侧边栏收起放出机制
        const wnav_frame = document.getElementById("wnav_frame");
        const wnav_list = document.getElementById("wnav_list");
        const wnav_hide = document.getElementById("wnav_hide");

        var wnav_hidden = true;
        function wnav_switchHide() {
            if (wnav_hidden) {
                wnav_frame.style.right = "0px";
            }
            else {
                wnav_frame.style.right = "-200px";
            }
            wnav_hidden = !wnav_hidden;
        }
        wnav_hide.addEventListener("click", wnav_switchHide)
    }
}
</script>

<template>
    <div id="wnav_frame">
        <div id="wnav_hide">
            日<br />志
        </div>
        <div id="wnav_list">
            <input v-model="msgStr" @keydown="send($event)" placeholder="输入消息" style="width: 100px;">
            <button @click="send($event)" ref="sendButton">发送</button>
            <br/>
            <div class="msgList">
                <div v-for="m in msgList" class="msg">
                    <div v-if="m.time" class="time">{{ m.time }}</div>
                    <div><span class="user">{{ m.user }}:</span>{{ m.str }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<style>
.time{
  color:gray;
  font-size:small;
}
.user{
  font-weight: bold;
  background-color: gray;
  color:white
}
.msg{
    background-color: #ccc;
    margin-top: 5px;
    border-radius: 5px;
}
.msgList{
    height: 300px;
    overflow-y:auto;
    border-top: 2px solid #aaa;
}
/* div.candDiv {
    width: fit-content;
    background-color: #aaa;
    border-radius: 5px;
    color: white;
    position: absolute;
}

div.candDiv div {
    cursor: pointer;
    padding: 2px;
}

div.candDiv div:hover {
    background-color: #888;
    border-radius: 5px;
} */

#wnav_hide {
    background-color: #ccc;
    float: left;
    width: 25px;
    height: 50px;
    margin-top: 330px;
    border-radius: 5px 0px 0px 5px;
    text-align: center;
    line-height: 25px;
    user-select: none;
    cursor: pointer;
    font-weight: bold;
    font-size: large;
    color: white;
    padding: 5px;
    box-shadow: 0px 0px 5px 0px #555;
}

#wnav_list {
    background-color: white;
    border: 2px solid #ccc;
    float: left;
    width: 186px;
    height: 400px;
    padding: 5px;
    font-size: large;
}

#wnav_frame {
    position: fixed;
    right: -200px;
    transition: 0.5s;
    bottom: 110px;
}
</style>